﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace HeathCareSystem
{
    public class TaiKhoanDAO
    {
        DataProvider dp = new DataProvider();

        public bool ThemTaiKhoanMoi(TaiKhoanDTO taiKhoan)
        {
            string strSQL = string.Format("INSERT INTO TaiKhoan (TenDangNhap, MatKhau, MaLoaiTaiKhoan, DangNhapLanDau, DaXoaTaiKhoan)" +
                                          "VALUES (N'{0}', N'{1}', '{2}', '{3}', '{4}')",
                                            taiKhoan.tenDangNhap, taiKhoan.matKhau, taiKhoan.maLoaiTaiKhoan, taiKhoan.dangNhapLanDau , taiKhoan.daXoaTaiKhoan);
            return dp.ExecuteNonQuery(strSQL);
        }

        public int LayMaTaiKhoanCuoi()
        {
            DataTable dt = new DataTable();
            string strSQL = string.Format("SELECT TOP 1 MaTaiKhoan FROM TAIKHOAN ORDER BY MaTaiKhoan DESC");
            dt = dp.ExecuteQuery(strSQL);
            return int.Parse(dt.Rows[0][0].ToString());
        }

        public bool CapNhatTaiKhoan(int MaTaiKhoan,string MatKhauMoi)
        {
            string strSQL = string.Format("UPDATE TaiKhoan SET MatKhau =  N'{0}', DangNhapLanDau = 'True'"+
                                          "WHERE (MaTaiKhoan = {1})",
                                            MatKhauMoi, MaTaiKhoan);
            return dp.ExecuteNonQuery(strSQL);
        }

        public bool XoaTaiKhoan(int maTaiKhoan, bool isXoa)
        {
            string strSQL = string.Format("UPDATE TaiKhoan SET DaXoaTaiKhoan =  '{0}' " +
                                          "WHERE (MaTaiKhoan = {1})",
                                            isXoa, maTaiKhoan);
            return dp.ExecuteNonQuery(strSQL);
        }
    }
}